﻿<UserControl x:Class="XmlEditor.Presentation.Views.XmlView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:Properties="clr-namespace:XmlEditor.Presentation.Properties"
             xmlns:Resources="clr-namespace:TreeListControl.Resources;assembly=TreeListControl"
             xmlns:Tree="clr-namespace:TreeListControl.Tree;assembly=TreeListControl"
             xmlns:Controls="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
             xmlns:c="clr-namespace:XmlEditor.Applications.Helpers;assembly=XmlEditor.Applications" 
             xmlns:UserControls="clr-namespace:XmlEditor.Presentation.UserControls" xmlns:ViewModels="clr-namespace:XmlEditor.Applications.ViewModels;assembly=XmlEditor.Applications">
    <UserControl.Resources>
        <c:CommandReference x:Key="UndoCommandReference"
                            Command="{Binding UndoCommand}" />
        <c:CommandReference x:Key="RedoCommandReference"
                            Command="{Binding RedoCommand}" />
        <c:CommandReference x:Key="CutNodeCommandReference"
                            Command="{Binding CutNodeCommand}" />
        <c:CommandReference x:Key="PasteNodeCommandReference"
                            Command="{Binding PasteNodeCommand}" />
        <c:CommandReference x:Key="CopyNodeCommandReference"
                            Command="{Binding CopyNodeCommand}" />
        <c:CommandReference x:Key="DeleteNodeCommandReference"
                            Command="{Binding DeleteNodeCommand}" />
        <c:CommandReference x:Key="SearchNextCommandReference"
                            Command="{Binding SearchNextNodeCommand}" />
        <c:CommandReference x:Key="SearchPreviousCommandReference"
                            Command="{Binding SearchPreviousNodeCommand}" />
        <!--Round tab-->
        <ControlTemplate x:Key="RoundTab"
                         TargetType="{x:Type TabItem}">
            <Grid SnapsToDevicePixels="True">
                <Border x:Name="Bd"
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="2,1,1,0"
                        CornerRadius="7,0,0,7"
                        Padding="{TemplateBinding Padding}">
                    <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                      HorizontalAlignment="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource FindAncestor, 
                        AncestorLevel=1, AncestorType={x:Type ItemsControl}}}"
                                      x:Name="Content"
                                      VerticalAlignment="{Binding Path=VerticalContentAlignment, 
                        RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type ItemsControl}}}"
                                      Content="{TemplateBinding Header}"
                                      ContentSource="Header"
                                      ContentTemplate="{TemplateBinding HeaderTemplate}"
                                      RecognizesAccessKey="True" />
                </Border>
            </Grid>
            <ControlTemplate.Triggers>

                <Trigger Property="IsMouseOver"
                         Value="True">
                    <Setter Property="Background"
                            TargetName="Bd">

                        <Setter.Value>
                            <LinearGradientBrush EndPoint="1,0"
                                                 StartPoint="0,0">
                                <GradientStop Color="#FFEAF6FD"
                                              Offset="0" />
                                <GradientStop Color="#FFA7D9F5"
                                              Offset="1" />
                            </LinearGradientBrush>
                        </Setter.Value>
                    </Setter>
                </Trigger>
                <Trigger Property="IsSelected"
                         Value="True">
                    <Setter Property="Panel.ZIndex"
                            Value="1" />
                    <Setter Property="Background"
                            TargetName="Bd"
                            Value="#FFFFFF" />
                </Trigger>
                <MultiTrigger>

                    <MultiTrigger.Conditions>
                        <Condition Property="IsSelected"
                                   Value="False" />
                        <Condition Property="IsMouseOver"
                                   Value="True" />
                    </MultiTrigger.Conditions>
                    <Setter Property="BorderThickness"
                            TargetName="Bd"
                            Value="3" />
                    <Setter Property="BorderBrush"
                            TargetName="Bd"
                            Value="#FF2B4E5B" />

                </MultiTrigger>
                <Trigger Property="TabStripPlacement"
                         Value="Bottom">
                    <Setter Property="BorderThickness"
                            TargetName="Bd"
                            Value="1,0,1,1" />
                </Trigger>
                <Trigger Property="TabStripPlacement"
                         Value="Left">
                    <Setter Property="BorderThickness"
                            TargetName="Bd"
                            Value="1,1,0,1" />
                </Trigger>
                <Trigger Property="TabStripPlacement"
                         Value="Right">
                    <Setter Property="BorderThickness"
                            TargetName="Bd"
                            Value="0,1,1,1" />
                </Trigger>
                <MultiTrigger>

                    <MultiTrigger.Conditions>
                        <Condition Property="IsSelected"
                                   Value="True" />
                        <Condition Property="TabStripPlacement"
                                   Value="Top" />
                    </MultiTrigger.Conditions>
                    <Setter Property="Margin"
                            Value="-2,-2,-2,-1" />
                    <Setter Property="Margin"
                            TargetName="Content"
                            Value="0,0,0,1" />
                </MultiTrigger>
                <MultiTrigger>

                    <MultiTrigger.Conditions>
                        <Condition Property="IsSelected"
                                   Value="True" />
                        <Condition Property="TabStripPlacement"
                                   Value="Bottom" />
                    </MultiTrigger.Conditions>
                    <Setter Property="Margin"
                            Value="-2,-1,-2,-2" />
                    <Setter Property="Margin"
                            TargetName="Content"
                            Value="0,1,0,0" />
                </MultiTrigger>
                <MultiTrigger>

                    <MultiTrigger.Conditions>
                        <Condition Property="IsSelected"
                                   Value="True" />
                        <Condition Property="TabStripPlacement"
                                   Value="Left" />
                    </MultiTrigger.Conditions>
                    <Setter Property="Margin"
                            Value="-2,-2,-1,-2" />
                    <Setter Property="Margin"
                            TargetName="Content"
                            Value="0,0,1,0" />
                </MultiTrigger>
                <MultiTrigger>

                    <MultiTrigger.Conditions>
                        <Condition Property="IsSelected"
                                   Value="True" />
                        <Condition Property="TabStripPlacement"
                                   Value="Right" />
                    </MultiTrigger.Conditions>
                    <Setter Property="Margin"
                            Value="-1,-2,-2,-2" />
                    <Setter Property="Margin"
                            TargetName="Content"
                            Value="1,0,0,0" />
                    <Setter Property="Background"
                            TargetName="Bd"
                            Value="#FF0000" />
                </MultiTrigger>
                <Trigger Property="IsEnabled"
                         Value="False">
                    <Setter Property="Background"
                            TargetName="Bd"
                            Value="#FF0000" />
                    <Setter Property="BorderBrush"
                            TargetName="Bd"
                            Value="#FFC9C7BA" />
                    <Setter Property="Foreground"
                            Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                </Trigger>
                <Trigger Property="IsSelected"
                         Value="False">
                    <Setter Property="Background"
                            TargetName="Bd"
                            Value="#EEEEEE" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
        <Resources:ConvertXmlNodeToEditHelp x:Key="convertXmlNodeToEditHelp" />
        <Resources:ConvertText x:Key="convertText" />

        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Validation.ErrorTemplate">
                <Setter.Value>
                    <ControlTemplate>
                        <DockPanel LastChildFill="True">
                            <TextBlock DockPanel.Dock="Right"
                                       Foreground="Red"
                                       FontSize="12pt"
                                       Text="! " />
                            <Border BorderBrush="Red"
                                    BorderThickness="1">
                                <AdornedElementPlaceholder />
                            </Border>
                        </DockPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="Validation.HasError"
                         Value="true">
                    <Setter Property="ToolTip"
                            Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />
                </Trigger>
            </Style.Triggers>
        </Style>

        <Resources:ConvertXmlNodeToComboBoxSource x:Key="convertXmlNodeToComboBoxSource" />
        <!--<Resources:ConvertNothing x:Key="convertNothing" />-->
        <Resources:ConvertXmlReferenceNodeToComboBoxSource x:Key="convertXmlReferenceNodeToComboBoxSource" />
        <Resources:ConvertXmlNodeToName x:Key="convertXmlNodeToName" />
        <Resources:ConvertXmlNodeToValue x:Key="convertXmlNodeToValue" />
        <Resources:ConvertUUIDToLabel x:Key="convertUUIDToLabel" />
        <Resources:ConvertToDateTime x:Key="convertToDateTime" />
        <Resources:ImageConverter x:Key="imageConverter"
                                  DefaultImage="\Resources\Images\RootIcon.png"
                                  AttributeImage="\Resources\Images\AttributeIcon.png"
                                  TextImage="\Resources\Images\TextIcon.png"
                                  ElementImage="\Resources\Images\ElementIcon.png"
                                  ClosedElementImage="\Resources\Images\ClosedIcon.png"
                                  OpenElementImage="\Resources\Images\OpenIcon.png"
                                  CommentImage="\Resources\Images\CommentIcon.png" />

        <Style TargetType="TextBox"
               x:Key="asd">
            <Setter Property="VerticalAlignment"
                    Value="Bottom" />
            <Setter Property="AcceptsReturn"
                    Value="True" />
            <Setter Property="Visibility"
                    Value="Collapsed" />
            <Setter Property="BorderThickness"
                    Value="0" />
            <Setter Property="BorderBrush"
                    Value="Transparent" />
            <Setter Property="Margin"
                    Value="0" />
            <Setter Property="Foreground"
                    Value="Blue" />
            <Style.Triggers>
                <Trigger Property="IsFocused"
                         Value="True">
                    <Setter Property="Cursor"
                            Value="IBeam" />
                </Trigger>
            </Style.Triggers>
        </Style>

        <Style TargetType="TextBlock"
               x:Key="textBlockStyle">
            <Setter Property="Foreground"
                    Value="Blue" />
            <Setter Property="Margin"
                    Value="4 1" />
        </Style>

        <DataTemplate x:Key="dateTemplate">
            <StackPanel>
                <TextBlock Text="{Binding Data}"
                           Foreground="Blue"
                           x:Name="nonEditable"
                           Margin="4 1" />
                <Controls:DatePicker SelectedDate="{Binding Data, Converter={StaticResource convertToDateTime}}"
                                     x:Name="editable"
                                     Visibility="Collapsed"
                                     BorderBrush="Transparent"
                                     Margin="0 -3"
                                     Foreground="Blue"
                                     SelectedDateFormat="Short" />
            </StackPanel>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
                                    AncestorType={x:Type ListBoxItem}, AncestorLevel=1}, Path=IsSelected}"
                             Value="True">
                    <Setter Property="Visibility"
                            Value="Collapsed"
                            TargetName="nonEditable" />
                    <Setter Property="Visibility"
                            Value="Visible"
                            TargetName="editable" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>

        <DataTemplate  x:Key="comboBoxTemplate">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Data}"
                           Foreground="Blue"
                           x:Name="nonEditable"
                           Margin="4 1" />
                <ComboBox x:Name="editable"
                          Visibility="Collapsed"
                          ItemsSource="{Binding ., Converter={StaticResource convertXmlNodeToComboBoxSource}}"
                          IsSynchronizedWithCurrentItem="True"
                          SelectedItem="{Binding Data}"
                          Width="{Binding ElementName=dataColumn, Path=Width}"
                          Margin="0 -2 -26 -2" />
            </StackPanel>
            <!--<ComboBox ItemsSource="{Binding Source={StaticResource myEnum}}" IsSynchronizedWithCurrentItem="True" />-->
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
                                    AncestorType={x:Type ListBoxItem}, AncestorLevel=1}, Path=IsSelected}"
                             Value="True">
                    <Setter Property="Visibility"
                            Value="Collapsed"
                            TargetName="nonEditable" />
                    <Setter Property="Visibility"
                            Value="Visible"
                            TargetName="editable" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>

        <DataTemplate  x:Key="comboBoxReferenceTemplate">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding ., Converter={StaticResource convertUUIDToLabel}}"
                           Foreground="Blue"
                           x:Name="nonEditable"
                           Margin="4 1" />
                <ComboBox x:Name="editable"
                          Visibility="Collapsed"
                          ItemsSource="{Binding ., Converter={StaticResource convertXmlReferenceNodeToComboBoxSource}}"
                          SelectedItem="{Binding Data, Converter={StaticResource convertXmlNodeToValue}}"
                          IsEditable="True"
                          IsSynchronizedWithCurrentItem="True"
                          IsTextSearchEnabled="True"
                          Width="{Binding ElementName=dataColumn, Path=Width}"
                          Margin="0 -2 -30 -2">
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding Path=ParentNode, StringFormat='\{0} ', Converter={StaticResource convertXmlNodeToName}}"
                                           FontStyle="Italic"
                                           FontWeight="Bold" />
                                <TextBlock Text="{Binding ParentNode.Name, StringFormat='\{0}: '}"
                                           FontStyle="Italic"
                                           FontWeight="Bold" />
                                <TextBlock Text="{Binding Data}" />
                            </StackPanel>
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
                </ComboBox>
            </StackPanel>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
                                    AncestorType={x:Type ListBoxItem}, AncestorLevel=1}, Path=IsSelected}"
                             Value="True">
                    <Setter Property="Visibility"
                            Value="Collapsed"
                            TargetName="nonEditable" />
                    <Setter Property="Visibility"
                            Value="Visible"
                            TargetName="editable" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>

        <DataTemplate x:Key="timeTemplate">
            <StackPanel Orientation="Horizontal"
                        HorizontalAlignment="Stretch"
                        ToolTip="{Binding ., Converter={StaticResource convertXmlNodeToEditHelp}}">
                <TextBlock Text="{Binding Data}"
                           Foreground="Blue"
                           x:Name="nonEditable"
                           Margin="4 1" />
                <TextBox x:Name="editable"
                         Text="{Binding Data}"
                         Visibility="Collapsed"
                         BorderThickness="0"
                         BorderBrush="Transparent"
                         Margin="0"
                         Width="{Binding ElementName=dataColumn, Path=Width}"
                         Foreground="Blue">
                    <Resources:WatermarkService.Watermark>
                        <TextBlock Text="hh:mm:ss.sss+hh:mm (msec and timezone is optional)"
                                   VerticalAlignment="Center" />
                    </Resources:WatermarkService.Watermark>
                </TextBox>
            </StackPanel>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
                                    AncestorType={x:Type ListBoxItem}, AncestorLevel=1}, Path=IsSelected}"
                             Value="True">
                    <Setter Property="Visibility"
                            Value="Collapsed"
                            TargetName="nonEditable" />
                    <Setter Property="Visibility"
                            Value="Visible"
                            TargetName="editable" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>

        <DataTemplate x:Key="textBlockTemplate" />

        <DataTemplate x:Key="defaultElementTemplate">
            <StackPanel Orientation="Horizontal"
                        HorizontalAlignment="Stretch"
                        ToolTip="{Binding ., Converter={StaticResource convertXmlNodeToEditHelp}}">
                <TextBlock Text="{Binding Data, Converter={StaticResource convertText}}"
                           x:Name="nonEditable"
                           Style="{StaticResource textBlockStyle}"
                           TextWrapping="Wrap"
                           TextTrimming="WordEllipsis" />
                <TextBox x:Name="editable"
                         Width="{Binding ElementName=dataColumn, Path=Width}"
                         Style="{StaticResource asd}"
                         IsVisibleChanged="EditableIsVisibleChanged">
                    <Binding Path="Data">
                        <Binding.ValidationRules>
                            <ExceptionValidationRule />
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox>
            </StackPanel>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
                                    AncestorType={x:Type ListBoxItem}, AncestorLevel=1}, Path=IsSelected}"
                             Value="True">
                    <Setter Property="Visibility"
                            Value="Collapsed"
                            TargetName="nonEditable" />
                    <Setter Property="Visibility"
                            Value="Visible"
                            TargetName="editable" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>

        <DataTemplate x:Key="defaultAttributeTemplate">
            <StackPanel Orientation="Horizontal"
                        HorizontalAlignment="Stretch"
                        ToolTip="{Binding ., Converter={StaticResource convertXmlNodeToEditHelp}}">
                <TextBlock Text="{Binding Value}"
                           x:Name="nonEditable"
                           Style="{StaticResource textBlockStyle}" />
                <TextBox x:Name="editable"
                         Width="{Binding ElementName=dataColumn, Path=Width}"
                         Style="{StaticResource asd}"
                         IsVisibleChanged="EditableIsVisibleChanged">
                    <Binding Path="Value">
                        <Binding.ValidationRules>
                            <ExceptionValidationRule />
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox>
            </StackPanel>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
                                    AncestorType={x:Type ListBoxItem}, AncestorLevel=1}, Path=IsSelected}"
                             Value="True">
                    <Setter Property="Visibility"
                            Value="Collapsed"
                            TargetName="nonEditable" />
                    <Setter Property="Visibility"
                            Value="Visible"
                            TargetName="editable" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
        <Resources:ConvertXmlNodeToListOfChildren x:Key="convertXmlNodeToListOfChildren" />

        <DataTemplate x:Key="MainMenuTemplate">
            <TextBlock Text="{Binding Name}"
                       ToolTip="{Binding Annotation}" />
        </DataTemplate>

    </UserControl.Resources>

    <UserControl.CommandBindings>
        <CommandBinding Command="ApplicationCommands.Undo"
                        x:Name="Undo"
                        Executed="UndoExecuted"
                        CanExecute="UndoCanExecute" />
        <CommandBinding Command="ApplicationCommands.Redo"
                        x:Name="Redo"
                        Executed="RedoExecuted"
                        CanExecute="RedoCanExecute" />
        <CommandBinding Command="Cut"
                        x:Name="Cut"
                        Executed="CutExecuted"
                        CanExecute="CutCanExecute" />
        <CommandBinding Command="Copy"
                        x:Name="Copy"
                        Executed="CopyExecuted"
                        CanExecute="CopyCanExecute" />
        <CommandBinding Command="Paste"
                        x:Name="Paste"
                        Executed="PasteExecuted"
                        CanExecute="PasteCanExecute" />
        <CommandBinding Command="EditingCommands.Delete"
                        x:Name="Delete"
                        Executed="DeleteExecuted"
                        CanExecute="DeleteCanExecute" />
    </UserControl.CommandBindings>

    <UserControl.InputBindings>
        <KeyBinding Key="Z"
                    Modifiers="Control"
                    Command="{StaticResource UndoCommandReference}" />
        <KeyBinding Key="Y"
                    Modifiers="Control"
                    Command="{StaticResource RedoCommandReference}" />
        <KeyBinding Key="C"
                    Modifiers="Control"
                    CommandParameter="{Binding ElementName=xmlTree, Path=SelectedItem}"
                    Command="{StaticResource CopyNodeCommandReference}" />
        <KeyBinding Key="X"
                    Modifiers="Control"
                    CommandParameter="{Binding ElementName=xmlTree, Path=SelectedItem}"
                    Command="{StaticResource CutNodeCommandReference}" />
        <KeyBinding Key="V"
                    Modifiers="Control"
                    CommandParameter="{Binding ElementName=xmlTree, Path=SelectedItem}"
                    Command="{StaticResource PasteNodeCommandReference}" />
        <KeyBinding Key="Delete"
                    Modifiers="Ctrl"
                    CommandParameter="{Binding ElementName=xmlTree, Path=SelectedItem}"
                    Command="{StaticResource DeleteNodeCommandReference}" />

        <KeyBinding Gesture="F3"
                    Command="Search"
                    CommandTarget="{Binding ElementName=SearchResults, Path=Content}" />
        <KeyBinding Gesture="Shift+F3"
                    Command="BrowseBack"
                    CommandTarget="{Binding ElementName=SearchResults, Path=Content}" />
    </UserControl.InputBindings>

    <!-- The XML view and custom panels -->
    <Grid PreviewKeyDown="GridPreviewKeyDown">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="{Binding ErrorMessagesTabHeight, Source={x:Static Properties:Settings.Default}, Mode=TwoWay}" />
        </Grid.RowDefinitions>

        <Tree:TreeList Grid.Row="0"
                       x:Name="xmlTree"
                       AlternationCount="2"
                       Model="{Binding XmlModel}"
                       SelectedNode="{Binding SelectedNode, Mode=TwoWay}">
            <Tree:TreeList.ItemContainerStyle>
                <Style>
                    <Style.Triggers>
                        <Trigger Property="ItemsControl.AlternationIndex"
                                 Value="1">
                            <Setter Property="Control.Background"
                                    Value="AliceBlue"></Setter>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Tree:TreeList.ItemContainerStyle>
            <Tree:TreeList.ContextMenu>
                <ContextMenu x:Name="contextMenu">
                    <MenuItem Header="Insert child..."
                              x:Name="insertNodeMenuItem"
                              Command="{Binding InsertNodeCommand}"
                              CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItem}"
                              ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItem, 
                        Converter={StaticResource convertXmlNodeToListOfChildren}}"
                              ItemTemplate="{StaticResource MainMenuTemplate}">
                        <MenuItem.ItemContainerStyle>
                            <Style>
                                <Setter Property="MenuItem.Command"
                                        Value="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=DataContext.InsertNodeCommand2}" />
                                <Setter Property="MenuItem.CommandParameter"
                                        Value="{Binding}" />
                            </Style>
                        </MenuItem.ItemContainerStyle>
                    </MenuItem>
                    <MenuItem Header="Insert sibling..."
                              x:Name="insertSiblingNodeMenuItem"
                              Command="{Binding InsertNodeCommand}"
                              CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItem.Parent}"
                              ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItem.Parent, 
                        Converter={StaticResource convertXmlNodeToListOfChildren}}"
                              ItemTemplate="{StaticResource MainMenuTemplate}">
                        <MenuItem.ItemContainerStyle>
                            <Style>
                                <Setter Property="MenuItem.Command"
                                        Value="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=DataContext.InsertNodeCommand2}" />
                                <Setter Property="MenuItem.CommandParameter"
                                        Value="{Binding}" />
                            </Style>
                        </MenuItem.ItemContainerStyle>
                    </MenuItem>
                    <MenuItem Header="Insert comment"
                              Command="{Binding InsertCommentCommand}"
                              CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItem}" />
                    <MenuItem Header="Delete"
                              InputGestureText="Ctrl+Del"
                              Command="{Binding DeleteNodeCommand}"
                              CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItem}" />
                    <Separator />
                    <MenuItem Header="Cut"
                              x:Name="cut"
                              InputGestureText="Ctrl+X"
                              Command="{Binding CutNodeCommand}"
                              CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItem}" />
                    <MenuItem Header="Copy"
                              x:Name="copy"
                              InputGestureText="Ctrl+C"
                              Command="{Binding CopyNodeCommand}"
                              CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItem}" />
                    <MenuItem Header="Paste"
                              x:Name="paste"
                              InputGestureText="Ctrl+V"
                              Command="{Binding PasteNodeCommand}"
                              CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItem}" />
                    <Separator />
                    <MenuItem Header="Undo"
                              Command="{Binding UndoCommand}"
                              InputGestureText="Ctrl+Z" />
                    <MenuItem Header="Redo"
                              Command="{Binding RedoCommand}"
                              InputGestureText="Ctrl+Y" />
                </ContextMenu>
            </Tree:TreeList.ContextMenu>
            <Tree:TreeList.View>
                <!--ColumnHeaderContainerStyle="{StaticResource hiddenStyle}"-->
                <GridView>
                    <GridView.Columns>
                        <GridViewColumn Width="400"
                                        Header="Name">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal">
                                        <Tree:RowExpander />
                                        <Image Source="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Tree:TreeListItem}}, 
                                            Converter={StaticResource imageConverter}}"
                                               Width="16"
                                               Height="16"
                                               Margin="0, 0, 5, 0" />
                                        <TextBlock Text="{Binding Name}" />
                                        <TextBlock Text=": " />
                                        <TextBlock Text="{Binding ., Converter={StaticResource convertXmlNodeToName}}"
                                                   FontStyle="Italic"
                                                   FontWeight="Bold" />
                                    </StackPanel>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>

                        <!--<GridViewColumn Width="2" CellTemplate="{StaticResource cellTemplate1}" />-->
                        <GridViewColumn x:Name="dataColumn"
                                        Header="Data"
                                        Width="400">
                            <GridViewColumn.CellTemplateSelector>
                                <Resources:CustomDataTemplateSelector DateTemplate="{StaticResource dateTemplate}"
                                                                      TimeTemplate="{StaticResource timeTemplate}"
                                                                      TextBlockTemplate="{StaticResource textBlockTemplate}"
                                                                      ComboBoxTemplate="{StaticResource comboBoxTemplate}"
                                                                      ComboBoxReferenceTemplate="{StaticResource comboBoxReferenceTemplate}"
                                                                      DefaultAttributeTemplate="{StaticResource defaultAttributeTemplate}"
                                                                      DefaultElementTemplate="{StaticResource defaultElementTemplate}" />
                            </GridViewColumn.CellTemplateSelector>
                        </GridViewColumn>
                        <!--<GridViewColumn Header="Edit help" CellTemplate="{StaticResource editHelpText}"/>-->
                        <!--<GridViewColumn Header="Annotation" DisplayMemberBinding="{Binding ., Converter={StaticResource xmlNodeToAnnotationConverter}}"/>-->
                        <!--<GridViewColumn Header="Annotation" Width="100" DisplayMemberBinding="{Binding SchemaInfo.SchemaElement.Annotation.Items[0].Markup[0].Value}"/>-->
                        <!--<GridViewColumn Header="Validity" Width="50" DisplayMemberBinding="{Binding SchemaInfo.Validity}"/>-->
                    </GridView.Columns>
                </GridView>
            </Tree:TreeList.View>
        </Tree:TreeList>

        <!--<TreeListControl:XmlEditorControl x:Name="xmlTree" 
                                          TreeListControl:XmlModel="{Binding XmlDocument}" />-->
        <!--XsltView="{Binding XsltView}" />-->

        <GridSplitter Grid.Row="1"
                      Grid.RowSpan="1"
                      Grid.ColumnSpan="1"
                      Height="10"
                      Width="Auto"
                      IsTabStop="False"
                      ResizeBehavior="BasedOnAlignment"
                      ResizeDirection="Rows"
                      HorizontalAlignment="Stretch"
                      VerticalAlignment="Stretch"
                      Margin="0" />

        <DockPanel LastChildFill="True"
                   Grid.Row="2"
                   x:Name="alternativeViews">
            <TabControl DockPanel.Dock="Bottom"
                        IsTabStop="False"
                        TabStripPlacement="Left">
                <TabControl.Resources>
                    <Style TargetType="{x:Type TabItem}">
                        <Setter Property="Padding"
                                Value="5" />
                        <Setter Property="HeaderTemplate">
                            <Setter.Value>
                                <DataTemplate>
                                    <ContentPresenter Content="{TemplateBinding Property=ContentControl.Content}" />
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </TabControl.Resources>
                <!--Dynamic help-->
                <TabItem Template="{StaticResource RoundTab}"
                         ToolTip="Dynamic help">
                    <TabItem.Header>
                        <Image Source="/eXed;component/Resources/Images/QuestionMark.png"
                               Height="16" />
                    </TabItem.Header>
                    <ContentControl Content="{Binding ElementName=xmlTree, Path=SelectedItem.Tag, Converter={StaticResource convertXmlNodeToEditHelp}}" />
                </TabItem>
            </TabControl>
            <TabControl DockPanel.Dock="Top"
                        SelectedIndex="{Binding SelectedViewIndex}"
                        IsTabStop="False"
                        TabStripPlacement="Left">
                <TabControl.Resources>
                    <Style TargetType="{x:Type TabItem}">
                        <Setter Property="Padding"
                                Value="5" />
                        <Setter Property="HeaderTemplate">
                            <Setter.Value>
                                <DataTemplate>
                                    <ContentPresenter Content="{TemplateBinding Property=ContentControl.Content}" />
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </TabControl.Resources>
                <!--Errors-->
                <TabItem x:Name="errors"
                         IsSelected="True"
                         Template="{StaticResource RoundTab}"
                         ToolTip="Errors">
                    <TabItem.Header>
                        <UserControls:GlassBall Diameter="16">
                            <UserControls:GlassBall.Style>
                                <Style TargetType="UserControls:GlassBall">
                                    <Setter Property="Color"
                                            Value="Red" />
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding ErrorMessages.Count}"
                                                     Value="0">
                                            <Setter Property="Color"
                                                    Value="DarkGreen" />
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </UserControls:GlassBall.Style>
                        </UserControls:GlassBall>
                        <!--<Ellipse Width="16"
                                 Height="16">
                            <Ellipse.Style>
                                <Style TargetType="Ellipse">
                                    <Setter Property="Fill"
                                            Value="Red" />
                                    <Setter Property="Stroke"
                                            Value="Black" />
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding ErrorMessages.Count}"
                                                     Value="0">
                                            <Setter Property="Fill"
                                                    Value="LightGreen" />
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </Ellipse.Style>
                        </Ellipse>-->
                    </TabItem.Header>
                    <ListView ItemsSource="{Binding ErrorMessages}"
                              SelectedItem="{Binding SelectedError}"
                              ScrollViewer.HorizontalScrollBarVisibility="Auto">
                        <!--<ListView.Resources>
                            <Style TargetType="{x:Type ListView}">
                                <Setter Property="ItemContainerStyle">
                                    <Setter.Value>
                                        <Style TargetType="ListViewItem">
                                            <Setter Property="VerticalContentAlignment"
                                                    Value="Top" />
                                        </Style>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </ListView.Resources>-->
                        <ListView.View>
                            <GridView>
                                <GridView.Columns>
                                    <GridViewColumn Header="Name"
                                                    DisplayMemberBinding="{Binding Name}"
                                                    Width="Auto" />
                                    <GridViewColumn Header="Value"
                                                    DisplayMemberBinding="{Binding Value}"
                                                    Width="100" />
                                    <GridViewColumn Header="Message"
                                                    Width="Auto">
                                        <GridViewColumn.CellTemplate>
                                            <DataTemplate>
                                                <TextBlock Text="{Binding Message}"
                                                           TextWrapping="Wrap" />
                                            </DataTemplate>
                                        </GridViewColumn.CellTemplate>
                                    </GridViewColumn>
                                </GridView.Columns>
                            </GridView>
                        </ListView.View>
                    </ListView>
                </TabItem>
                <TabItem x:Name="view"
                         Template="{StaticResource RoundTab}"
                         Content="{Binding XsltView}"
                         ToolTip="XSLT View">
                    <TabItem.Header>
                        <Image Source="/eXed;component/Resources/Images/Eye.png"
                               Height="16" />
                    </TabItem.Header>
                </TabItem>
                <TabItem x:Name="SearchResults"
                         Template="{StaticResource RoundTab}"
                         Content="{Binding SearchView}"
                         ToolTip="Search results">
                    <TabItem.Header>
                        <Image Source="/eXed;component/Resources/Images/find.png"
                               Height="16" />
                    </TabItem.Header>
                    <!--<ListView ItemsSource="{Binding FoundNodes}"
                              SelectedItem="{Binding SelectedFoundNode}">
                        <ListView.Resources>
                            <Style TargetType="{x:Type ListView}">
                                <Setter Property="ItemContainerStyle">
                                    <Setter.Value>
                                        <Style TargetType="ListViewItem">
                                            <Setter Property="VerticalContentAlignment"
                                                    Value="Top" />
                                        </Style>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </ListView.Resources>
                        <ListView.View>
                            <GridView>
                                <GridView.Columns>
                                    <GridViewColumn Header="Name"
                                                    DisplayMemberBinding="{Binding Name}"
                                                    Width="Auto" />
                                    <GridViewColumn Header="Value"
                                                    DisplayMemberBinding="{Binding Value}"
                                                    Width="100" />
                                </GridView.Columns>
                            </GridView>
                        </ListView.View>
                    </ListView>-->
                </TabItem>
            </TabControl>
        </DockPanel>

    </Grid>
</UserControl>
